package com.pachong.android.framework.search;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.flexbox.FlexboxLayout;
import com.pachong.android.framework.R;
import com.pachong.android.framework.search.adapter.HotSearchAdapter;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseSearchActivity {

    private List<String> mDataHistory = new ArrayList<>();
    private EditText mEdSearch;
    private SearchListView mSearchListview;
    private ListAdapter mListAdapter;
    private TextView mTvClean;
    private ImageView mImgCleanEdit;
    private TextView mTvCannel;
    private SearchFlexBoxLayout mFblHotSearch;

    //测试用数组
    private String[] mVals = new String[]
            {"Android", "IOS", "PHP", "C#", "运营", "编辑", "画手", "JAVA", "实习前端开发"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 接收热门消息容器
     */
    @Override
    public List<String> getHotSearchInfoes() {
        return null;
    }


    /**
     * 接收历史消息容器
     */
    @Override
    public List<String> getHistorySearchInfoes() {
        return null;
    }

    @Override
    public View createDataView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_search, container, false);
        initView(view);
        setViewAdapter(inflater);
        setViewClickListener();
        return view;
    }

    private void setViewAdapter(final LayoutInflater inflater) {
        mFblHotSearch.setAdapter(new HotTagAdapter(inflater));
        mListAdapter = new ListAdapter();
        mSearchListview.setAdapter(mListAdapter);
        mListAdapter.notifyDataSetChanged();
    }

    private void initView(View view) {
        mSearchListview = (SearchListView) view.findViewById(R.id.LvSearchHistroy);
        mEdSearch = (EditText) view.findViewById(R.id.ed_search);
        mTvClean = (TextView) view.findViewById(R.id.tvSearchClen);
        mImgCleanEdit = (ImageView) view.findViewById(R.id.img_search_claen);
        mFblHotSearch = (SearchFlexBoxLayout) view.findViewById(R.id.fbl_search);
        mTvCannel = (TextView)view.findViewById(R.id.tv_search_cannel);
        //判断容器大小显示隐藏清除按钮
        if (mDataHistory.size() == 0) {
            mTvClean.setVisibility(View.GONE);
        } else {
            mTvClean.setVisibility(View.VISIBLE);
        }
    }

    private void setViewClickListener() {
        mEdSearch.setOnKeyListener(new EditOnKeyListener());
        mSearchListview.setOnItemClickListener(new SearchListOnItemClickListener());
        mImgCleanEdit.setOnClickListener(new CleanTextOnClickListener());
        mTvClean.setOnClickListener(new CleanHisOnClickListener());
        mTvCannel.setOnClickListener(new CannelListener());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    private class ListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mDataHistory.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.item_search_result, null);
            TextView tvResult = (TextView) view.findViewById(R.id.tvSearchResult);
            tvResult.setText(mDataHistory.get(position));
            Drawable history_clock = getResources().getDrawable(R.mipmap.history_clock);
            history_clock.setBounds(0, 0, 12, 12);
            tvResult.setCompoundDrawables(history_clock, null, null, null);
            return view;
        }
    }

    private class EditOnKeyListener implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 修改回车键功能
                // 先隐藏键盘
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                String searchText = mEdSearch.getText().toString().trim();
                mDataHistory.add(searchText);
                mListAdapter.notifyDataSetChanged();
                mEdSearch.setText("");
                mTvClean.setVisibility(View.VISIBLE);

                Toast.makeText(SearchActivity.this, "查询内容 :" + searchText, Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    }

    private class SearchListOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView tvContent = (TextView) view.findViewById(R.id.tvSearchResult);
            String content = tvContent.getText().toString();
            mEdSearch.setText(content);

            // TODO 点击搜索结果回调
            Toast.makeText(SearchActivity.this, "历史记录 : " + content, Toast.LENGTH_SHORT).show();

        }
    }

    private class CleanTextOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mEdSearch.setText("");
        }
    }

    private class CleanHisOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mDataHistory.clear();
            mListAdapter.notifyDataSetChanged();
            mTvClean.setVisibility(View.GONE);
        }
    }

    private class HotTagAdapter extends HotSearchAdapter<String> {
        private final LayoutInflater inflater;

        public HotTagAdapter(LayoutInflater inflater) {
            super(SearchActivity.this.mVals);
            this.inflater = inflater;
        }

        @Override
        public View getView(ViewGroup parent, final int position, String text) {
            TextView tvHotTag = (TextView) inflater.inflate(R.layout.item_hot_search, parent, false);
            tvHotTag.setText(text);
            tvHotTag.setOnClickListener(new HotTagListener(position));
            return tvHotTag;
        }

        private class HotTagListener implements View.OnClickListener {
            private final int position;

            public HotTagListener(int position) {
                this.position = position;
            }

            @Override
            public void onClick(View v) {
                //TODO 点击热门搜索标签回调
                mEdSearch.setText(mVals[position]);
                Toast.makeText(getBaseContext(), "热门标签 :" + mVals[position], Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class CannelListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            finish();
        }
    }
}
